package com.icloud.admin.domain;


import com.icloud.framework.domain.model.AuditingEntity;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;

@Entity(name = "sys_menus")
@EntityListeners(AuditingEntityListener.class)
public class Menu extends AuditingEntity {


    private String name;
    private String href;
    private Integer position;
    private String icon;
    private String category;
    private String frontComponent;
    private String description;
    private boolean enabled;

    @OneToOne
    @JoinColumn(name = "parent_id")
    private Menu parent;

    @ManyToMany(cascade = CascadeType.PERSIST, fetch = FetchType.LAZY)
    @JoinTable(name = "sys_menu_resource_relation",
            joinColumns = {@JoinColumn(name = "menu_id", referencedColumnName = "id")},
            inverseJoinColumns = {@JoinColumn(name = "resorce_id", referencedColumnName = "id")})
    private Set<Resource> resources = new HashSet();

    public Menu() {
    }


    /**
     * 全参构造方法
     *
     * @param parent
     * @param name
     * @param href
     * @param position
     * @param icon
     * @param category
     * @param frontComponent
     * @param description
     */
    public Menu(Menu parent, String name, String href, Integer position, String icon, String category, String frontComponent, String description) {
        this.parent = parent;
        this.name = name;
        this.href = href;
        this.position = position;
        this.icon = icon;
        this.category = category;
        this.frontComponent = frontComponent;
        this.description = description;
        this.enabled = true;
    }

    public void changePosition(Integer position) {
        this.position = position;
    }


    /**
     * 更新属性
     *
     * @param parent
     * @param name
     * @param href
     * @param position
     * @param icon
     * @param category
     * @param frontComponent
     * @param description
     */
    public void changePops(Menu parent, String name, String href, Integer position, String icon, String category, String frontComponent, String description) {
        this.parent = parent;
        this.name = name;
        this.href = href;
        this.position = position;
        this.icon = icon;
        this.category = category;
        this.frontComponent = frontComponent;
        this.description = description;
    }

    public Menu getParent() {
        return this.parent;
    }

    /**
     * 新增资源
     *
     * @param resource
     */
    public void addResource(Resource resource) {
        this.resources.add(resource);
    }

    /**
     * 删除资源
     *
     * @param resource
     */
    public void removeResource(Resource resource) {
        this.resources.remove(resource);
    }

    public void disable() {
        this.enabled = false;
    }
}
